﻿@using Newtouch.Herp.Infrastructure.Enum;
@using Newtouch.Tools
@{
    ViewBag.Title = "OutStorage";
    Layout = "~/Views/Shared/_Index.cshtml";
}
<style>
    .req {
        color: red;
    }
</style>
<form id="form1">
    <div class="panel panel-default">
        <div class="panel-heading">出库信息</div>
        <div style="padding-right:10px;">
            <table class="form">
                <tr>
                    <th class="formTitle">出库单号：</th>
                    <td class="formValue">
                        <input id="Pdh" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">配送单号：</th>
                    <td class="formValue">
                        <input type="text" class="form-control" id="deliveryNo" placeholder="配送单号" />
                    </td>
                    <th class="formTitle">出库方式：</th>
                    <td class="formValue">
                        <select id="crkfs" class="form-control required"></select>
                    </td>
                    <th class="formTitle"><span class="req">*</span>供应商名称：</th>
                    <td class="formValue">
                        <input id="rkbmName" type="text" class="form-control" />
                        <input id="rkbmId" type="hidden" value="" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="panel-heading">物资信息</div>
        <div style="padding-right:10px;">
            <table class="form">
                <tr>
                    <th class="formTitle"><span class="req">*</span>物资：</th>
                    <td class="formValue">
                        <input id="wzmc" type="text" class="form-control" placeholder="代码 名称 拼音 商品名" />
                        <input id="productId" type="hidden" value="" />
                        <input id="editRowId" type="hidden" value="" />
                        <input id="lbmc" type="hidden" value="" />
                    </td>
                    <th class="formTitle"><span class="req">*</span>批次：</th>
                    <td class="formValue">
                        <input id="pc" type="text" class="form-control" />
                    </td>
                    <th class="formTitle" id="slDW"><span class="req">*</span>出库数量：</th>
                    <td class="formValue">
                        <div class="row">
                            <div class="col-md-8">
                                <input id="sl" type="text" class="form-control" onchange="CKSL_Leave()" />
                            </div>
                            <div class="col-md-4">
                                <select id="dw" class="form-control" onchange="CKSL_Leave()"></select>
                                <input id="mindwmc" value="" data-zhyz="" data-name="" type="hidden" />
                                <input id="bmdwmc" value="" data-zhyz="" data-name="" type="hidden" />
                            </div>
                        </div>
                    </td>
                    <th class="formTitle"><span class="req">*</span>出库金额：</th>
                    <td class="formValue">
                        <input id="zje" type="text" style="width:70%;float: left;" class="form-control" onchange="CKJE_Leave()" />
                        <span style="line-height:24px;margin-left: 10px;">(元)</span>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">备注：</th>
                    <td class="formValue">
                        <input id="remark" type="text" class="form-control" />
                    </td>
                    <th class="formTitle">批号：</th>
                    <td class="formValue">
                        <input id="ph" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">可用库存：</th>
                    <td class="formValue">
                        <input id="slstr" type="text" class="form-control newtouch_Readonly" />
                        <input id="kykcsl" type="text" hidden="hidden" />
                    </td>
                    <th class="formTitle" id="thJJDW">出库单价：</th>
                    <td class="formValue">
                        <input id="jj" type="text" style="width: 70%;float: left;" class="form-control newtouch_Readonly" />
                        <span style="line-height:24px;margin-left: 10px;">(元)</span>
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">规格：</th>
                    <td class="formValue">
                        <input id="gg" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">零售价：</th>
                    <td class="formValue">
                        <input id="lsj" type="text" style="width:65%;float: left;" class="form-control newtouch_Readonly" />
                        <span name="sp_bmdw" style="line-height:24px;margin-left: 10px;"></span>
                        <input id="minlsj" type="hidden" value="" />
                    </td>
                    <th class="formTitle">零售总额：</th>
                    <td class="formValue">
                        <input id="lsze" type="text" style="width:70%;float: left;" class="form-control newtouch_Readonly" />
                        <span style="line-height:24px;margin-left: 10px;">(元)</span>
                    </td>
                    <th class="formTitle">转化因子：</th>
                    <td class="formValue">
                        <input id="zhyz" type="text" class="form-control newtouch_Readonly" />
                    </td>
                </tr>
                <tr>
                    <th class="formTitle">有效期：</th>
                    <td class="formValue">
                        <input id="yxq" type="text" class="form-control input-wdatepicker newtouch_Readonly" value="@DateTime.Now.ToString("yyyy-MM-dd")" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" />
                    </td>
                    <th class="formTitle">生产厂家：</th>
                    <td class="formValue">
                        <input id="sccj" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <th class="formTitle">生产日期：</th>
                    <td class="formValue">
                        <input id="scrq" type="text" class="form-control newtouch_Readonly" />
                    </td>
                    <td colspan="2"></td>
                </tr>
            </table>
        </div>
    </div>
    <input id="fph" type="hidden" value="" />
</form>

@Html.Partial("_MiddleButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new int[] { 2, 3, 6 },
    F2Text = "添加",
    F3Text = "修改",
    F6Text = "删除"
})

<div class="gridPanel">
    <table id="gridCKMX"></table>
    <div id="gridPager" rel="form1"></div>
</div>

@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new int[] { 7, 8 },
    F7Text = "重置",
    F8Text = "出库"
})
<span style="float: left;line-height: 25px;margin: 8px 5px;">出库总额：&nbsp;<span id="txtCKZE"></span> &nbsp;元</span>
<script>
    $(function () {
        //浏览器窗口调整大小时重新加载jqGrid的宽
        $(window).resize(function () {
            window.initLayout("MyTabGrid");
        });
        gridCKMX([]);
        initialCKFS();//加载出库方式
        initialDeliveryNO();//注册配送单浮层
        initialGYS();//初始化供应商
        newtouch_event_f7();
        $("#wzmc").change(function () {
            $("#ph").val("");
            $("#pc").val("");
            $("#slstr").val("");
            $("#kykcsl").attr('data-kcsl', "").val("");//kykcsl
            $("#productId").val("");
        });
        BackspaceGYS();
    });

    //物资供应商
    function initialGYS() {
        $("#rkbmName").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            url: '/Storage/GetSupplierList',
            height: 300,
            width: 300,
            caption: "物资供应商",
            ajaxparameters: function () {
                return "key=" + $("#rkbmName").val();
            },
            colModel: [
                { label: 'Id', name: 'Id', hidden: true },
                { label: '供应商', name: 'name', widthratio: 80 }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#rkbmName").val($thistr.attr('data-name'));
                $("#rkbmId").val($thistr.attr('data-Id'));
            }
        });
    }

    //删除清楚供应商
    function BackspaceGYS() {
        $("#rkbmName").keydown(function (e) {
            var e = window.event ? window.event : e;
            var keyCode = e.which ? e.which : e.keyCode;
            if (keyCode == 8) {//tab
                $("#rkbmName").val("");
                $("#rkbmId").val("");
            }
        });
    }

    //新出库
    function newtouch_event_f7() {
        GetNewCkdh();//换单号
        EnabledSysBasicInfo();//启用
        CleanInput();//清空明细
        $('#gridCKMX').jqGrid("clearGridData");
        $("#txtCKZE").html("");
    }

    //获取明细
    function getEditRowData() {
        return [{
            Pdh: $("#Pdh").val(),
            deliveryNo: $("#deliveryNo").val(),
            crkfs: $("#crkfs").val(),
            rkbmName: $("#rkbmName").val(),
            rkbmId: $("#rkbmId").val(),
            lbmc: $("#lbmc").val(),
            wzmc: $("#wzmc").val(),
            gg: $("#gg").val(),
            sl: parseFloat($("#sl").val()),   //最后保存到数据库的数量
            unitName: $("#dw").find("option:selected").text(),
            zhyz: $("#dw").find("option:selected").attr("data-zhyz"),
            ph: $("#ph").val(),
            pc: $("#pc").val(),
            yxq: $("#yxq").val(),
            lsj: $("#lsj").val(),
            minlsj: $("#minlsj").val(),
            lsze: $("#lsze").val(),
            jj: $("#jj").val(),
            zje: $("#zje").val(),
            slstr: $("#slstr").val(),
            remark: $("#remark").val(),
            sccj: $("#sccj").val(),
            scrq: $("#scrq").val(),
            productId: $("#productId").val(),
            ckbmkc: $("#kykcsl").attr("data-kcsl"),
            kykcsl: $("#kykcsl").val(),
            zxdwId: $("#mindwmc").val(),
            mindwmc: $("#mindwmc").attr("data-name"),
            zxdwzhyz: $("#mindwmc").attr("data-zhyz"),
            bmdwId: $("#bmdwmc").val(),
            bmdwmc: $("#bmdwmc").attr("data-name"),
            bmdwzhyz: $("#bmdwmc").attr("data-zhyz"),
            unitId: $("#dw").val(),
            fph: $("#fph").val(),
            ckbm: $("#supplierId").val()
        }];
    }

    //加载明细
    function gridCKMX(mydata) {
        var $gridCkmx = $("#gridCKMX");
        $gridCkmx.newtouchLocalDataGrid({
            height: $(window).height() - document.getElementById('gridCKMX').offsetTop - 85,
            unwritten: false,
            colModel: [
                { label: '物资类型', name: 'lbmc', width: 70, align: 'left' },
                { label: '配送单号', name: 'deliveryNo', width: 80, align: 'left' },
                { label: '物资名称', name: 'wzmc', width: 110, align: 'left' },
                { label: '规格', name: 'gg', width: 80, align: 'left' },
                { label: '数量', name: 'sl', width: 60, align: 'left' },
                { label: '单位', name: 'unitName', width: 50, align: 'center' },
                { label: '现有库存', name: 'slstr', width: 70, align: 'right' },
                { label: '零售单价', name: 'lsj', width: 70, align: 'right' },
                { label: '零售总额', name: 'lsze', width: 70, align: 'right' },
                { label: '出库单价', name: 'jj', width: 70, align: 'right' },
                {
                    label: '出库金额', name: 'zje', width: 80, align: 'right',
                    formatter: function (cellvalue) {
                        return cellvalue ? parseFloat(cellvalue).toFixed(2) : "0.00";
                    }
                },
                { label: '转化因子', name: 'zhyz', width: 60, align: 'center' },
                { label: '批号', name: 'ph', width: 100, align: 'center' },
                { label: '批次', name: 'pc', width: 100, align: 'center' },
                { label: '有效期', name: 'yxq', width: 70, align: 'center' },
                { label: '备注', name: 'remark', width: 100, align: 'left' },
                { label: '生产厂家', name: 'sccj', width: 100, align: 'left' },
                { label: '生产日期', name: 'scrq', width: 70, align: 'left' },
                { label: 'productId', name: 'productId', hidden: true },
                { label: "kykcsl", name: "kykcsl", hidden: true },
                { label: 'bmdwmc', name: 'bmdwmc', hidden: true },
                { label: 'ckbmkc', name: 'ckbmkc', hidden: true },
                { label: 'mindwmc', name: 'mindwmc', hidden: true },
                { label: 'zxdwId', name: 'zxdwId', hidden: true },
                { label: 'zxdwzhyz', name: 'zxdwzhyz', hidden: true },
                { label: 'bmdwId', name: 'bmdwId', hidden: true },
                { label: 'bmdwzhyz', name: 'bmdwzhyz', hidden: true },
                { label: 'unitId', name: 'unitId', hidden: true },
                { label: 'Pdh', name: 'Pdh', hidden: true },
                { label: 'crkfs', name: 'crkfs', hidden: true },
                { label: 'rkbmName', name: 'rkbmName', hidden: true },
                { label: 'minlsj', name: 'minlsj', hidden: true },
                { label: 'fph', name: 'fph', hidden: true },
                { label: 'rkbmId', name: 'rkbmId', hidden: true }

            ],
            ondblClickRow: function (rowid) {
                gridDbclickRow(rowid);
            }
        }, mydata);
    }

    //双击
    function gridDbclickRow(rowid) {
        if (rowid) {
            var getRowData = $("#gridCKMX").jqGrid('getRowData', rowid);
            if (getRowData) {
                $("#Pdh").val(getRowData.Pdh);
                $("#crkfs").val(getRowData.crkfs);
                $("#rkbmName").val(getRowData.rkbmName);
                $("#rkbmId").val(getRowData.rkbmId);
                $("#dw").empty();
                if (getRowData.mindwmc !== getRowData.bmdwmc) {
                    $("#dw").append("<option data-zhyz='1' value='" + getRowData.zxdwId + "'>" + getRowData.mindwmc + "</option>");   //出库单位
                }
                $("#dw").append("<option data-zhyz='" + getRowData.bmdwzhyz + "' value='" + getRowData.bmdwId + "'>" + getRowData.bmdwmc + "</option>");   //出库单位
                $("#mindwmc").val(getRowData.zxdwId).attr("data-zhyz", 1).attr("data-name", getRowData.mindwmc);
                $("#bmdwmc").val(getRowData.bmdwId).attr("data-zhyz", getRowData.bmdwzhyz).attr("data-name", getRowData.bmdwmc);
                $("#lbmc").val(getRowData.lbmc);
                $("#deliveryNo").val(getRowData.deliveryNo);
                $("#wzmc").val(getRowData.wzmc);
                $("#gg").val(getRowData.gg);
                $("#sl").val(getRowData.sl);
                $("#zhyz").val(getRowData.zhyz);
                $("#ph").val(getRowData.ph);
                $("#pc").val(getRowData.pc);
                $("#yxq").val(getRowData.yxq);
                $("#lsj").val(getRowData.lsj);
                $("#lsze").val(getRowData.lsze);
                $("#jj").val(getRowData.jj);
                $("#zje").val(getRowData.zje);
                $("#slstr").val(getRowData.slstr);
                $("#remark").val(getRowData.remark);
                $("#sccj").val(getRowData.sccj);
                $("#scrq").val(getRowData.scrq);
                $("#productId").val(getRowData.productId);
                $("#kykcsl").val(getRowData.kykcsl).attr("data-kcsl", getRowData.ckbmkc);
                $("#supplierId").val(getRowData.ckbm);
                $("#minlsj").val(getRowData.minlsj);
                $("#fph").val(getRowData.fph);
                $("#dw").val(getRowData.unitId).trigger("change");
                RemarkEditRowId(rowid)
            }
        }
    }

    //添加按钮
    function newtouch_event_f2() {
        if (!DataValate()) {//数据校验
            return false;
        }
        var data = getEditRowData();//获取明细
        if (data === false) return false;
        //检查重复项
        var isRepeatedAdd = false;
        $.each($("#gridCKMX").getDataIDs(), function () {
            var rowData = $("#gridCKMX").jqGrid('getRowData', this);
            if (rowData.productId === data[0].productId && rowData.ph === data[0].ph && rowData.pc === data[0].pc) {
                isRepeatedAdd = true;
            }
        });
        if (isRepeatedAdd) {
            $.modalAlert("不可重复添加", 'warning');
            return false;
        }
        $("#gridCKMX").newtouchLocalDataGrid(null, data);
        ComputeCKZE();//计算出库金额
        DisableControls();//禁用
        ChangeDeliveryNo();//清空明细
        return true;
    }

    //修改按钮
    function newtouch_event_f3() {
        if ($('#editRowId').val()) {//数据校验
            if (!DataValate()) {
                return false;
            }
            var data = getEditRowData();//获取明细
            $('#gridCKMX').jqGrid("delRowData", $('#editRowId').val()); //先remove掉那一行
            //检查重复项
            var isRepeatedAdd = false;
            $.each($("#gridCKMX").getDataIDs(), function () {
                var rowData = $("#gridCKMX").jqGrid('getRowData', this);
                if (rowData.productId === data[0].productId && rowData.ph === data[0].ph && rowData.pc === data[0].pc) {
                    isRepeatedAdd = true;
                }
            });
            if (isRepeatedAdd) {
                $.modalAlert("重复添加", 'warning');
                return false;
            }
            $("#gridCKMX").newtouchLocalDataGrid(null, data);
            ComputeCKZE();//计算出库金额
            DisableControls();//禁用
            ChangeDeliveryNo();//清空明细
        }
        else {//获取grid当前选中的那一行
            var seleId = $('#gridCKMX').jqGrid('getGridParam', 'selrow');
            if (seleId) {
                gridDbclickRow(seleId);//请求修改该行
            }
        }
    }

    //删除
    function newtouch_event_f6() {
        var delRowid = $("#gridCKMX").jqGrid('getGridParam', 'selrow');
        if (!delRowid) {
            $.modalAlert("未选中行", 'warning');
            return;
        }
        $('#gridCKMX').jqGrid('delRowData', delRowid);
        ComputeCKZE();//计算入库总额
        ChangeDeliveryNo();//清空明细
    }

    //出库
    function newtouch_event_f8() {
        var djmx = $("#gridCKMX").jqGrid('getRowData_AllLine');
        if (djmx == null || djmx.length <= 0) {
            $.modalAlert("请先添加出库信息", 'warning');
            return;
        }
        var crkdj = Object();
        crkdj.deliveryNo = djmx[0].deliveryNo;
        crkdj.pdh = djmx[0].Pdh;
        crkdj.rkbm = djmx[0].rkbmId;
        crkdj.crkfs = djmx[0].crkfs;
        $.najax({
            url: "/StorageManage/Storage/SaveOutStorageInfo",
            dataType: "json",
            data: { crkdj: crkdj, crkdjmx: djmx },
            type: "POST",
            success: function (res) {
                if (res.state === "success") {
                    $.modalAlert("出库成功", 'success');
                    newtouch_event_f7();//出入库：新单据号 主数据清空 明细清空
                    $('#gridCKMX').jqGrid("clearGridData");//清空Grid
                } else {
                    $.modalAlert(res.message, 'error');
                }
            }
        });
    }

    //加载出库单号
    function GetNewCkdh() {
        $.najax({
            url: "/StorageManage/Storage/GetNewCkdh?r=" + Math.random(),
            dataType: "text",
            type: "Get",
            success: function (data) {
                $("#Pdh").val(data);
            }
        });
    }

    //出库方式
    function initialCKFS() {
        $("#crkfs").bindSelect({
            url: "/Storage/GetStorageIOMode?crkbz=1",
            id: "Id",
            text: "crkfsmc"
        });
    }

    //配送单号
    function initialDeliveryNO() {
        $("#deliveryNo").newtouchBatchFloatingSelector({
            clickautotrigger: true,
            url: '/DeliveryManage/DeliveryQuery/OutStorageDeliveryQuery',
            height: 300,
            width: 900,
            caption: "配送单号",
            ajaxparameters: function () {
                return "keyword=" + $("#deliveryNo").val();
            },
            colModel: [
                { label: '配送单号', name: 'deliveryNo', width: 150, align: "center" },
                { label: '单据号', name: 'djh', width: 180, align: "center" },
                {
                    label: '审核状态', name: 'shzt', width: 80, align: 'left', formatter: function (val) {
                        switch (val) {
                        case '@((int)EnumAuditState.Temporary)':
                            return "@EnumAuditState.Temporary.GetDescription()";
                        case '@((int)EnumAuditState.Adopt)':
                            return "@EnumAuditState.Adopt.GetDescription()";
                        case '@((int)EnumAuditState.Cancelled)':
                            return "@EnumAuditState.Cancelled.GetDescription()";
                        case '@((int)EnumAuditState.Refuse)':
                            return "@EnumAuditState.Refuse.GetDescription()";
                        case '@((int)EnumAuditState.Waiting)':
                            return "@EnumAuditState.Waiting.GetDescription()";
                        default:
                            return "";
                        }
                    }
                },
                { label: '供应商名称', name: 'gysmc', width: 180, align: "center" },
                { label: '出入库方式', name: 'crkfsmc', width: 80, align: "center" },
                { label: '创建时间', name: 'CreateTime', width: 130, align: "center", formatter: "date", formatoptions: { srcformat: 'Y-m-d H:i:s', newformat: 'Y-m-d H:i:s' } },
                { label: '创建人', name: 'CreatorCode', width: 80, align: "center" },
                { label: 'gysId', name: 'gysId', hidden: true },
                { label: 'crkfsId', name: 'crkfsId', hidden: true }
            ],
            itemdbclickhandler: function ($thistr) {
                $("#deliveryNo").val($thistr.attr('data-deliveryNo'));
                $("#rkbmId").val($thistr.attr('data-gysId'));
                $("#rkbmName").val($thistr.attr('data-gysmc'));
                ChangeDeliveryNo();//清空明细
            }
        });
    }

    //输入码
    $("#wzmc").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 300,
        width: 900,
        id: 'wzsrm',
        url: '/StorageManage/Storage/QueryStockListByGys',
        ajaxmethod: "Get",
        inputtextcheck: function () {
            if ($("#rkbmId").val().replace(/(^\s*)|(\s*$)/g, "") === "" || $("#rkbmName").val().replace(/(^\s*)|(\s*$)/g, "") === "") {   //去掉左右两端空格
                $.modalAlert("请先选择供应商", 'warning');
                return false;
            }
            return true;
        },
        ajaxreqdata: function () {
            var reqData = {};
            reqData.key = $("#wzmc").val();
            reqData.deliveryNo = $("#deliveryNo").val();
            reqData.gysId = $("#rkbmId").val();
            return reqData;
        },
        caption: "物资信息",
        colModel: [
            { label: '类别', name: 'lbmc', width: 120 },
            { label: '名称', name: 'name', width: 200 },
            { label: '库存', name: 'slstr', width: 100 },
            { label: '规格', name: 'gg', width: 120 },
            {
                label: '零售价', name: 'bmlsj', width: 90, align: "right",
                formatter: function (cellvalue) {
                    return cellvalue ? parseFloat(cellvalue).toFixed(4) : "0.00";
                }
            },
            { label: '单位', name: 'bmdwmc', width: 60 },
            { label: '生产厂家', name: 'supplierName', width: 180,align:"left" },
            { label: 'Id', name: 'Id', hidden: true },
            { label: 'kykcsl', name: 'kykcsl', hidden: true },
            { label: 'zhyz', name: 'zhyz', hidden: true },
            { label: 'bmdwmc', name: 'bmdwmc', hidden: true },
            { label: 'bmdwId', name: 'bmdwId', hidden: true },
            { label: 'zxdwId', name: 'zxdwId', hidden: true },
            { label: 'minlsj', name: 'minlsj', hidden: true },
            { label: 'mindwmc', name: 'mindwmc', hidden: true }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#wzmc").val($thistr.attr('data-name'));
            $("#productId").val($thistr.attr('data-Id'));
            $("#dw").empty();
            if ($thistr.attr('data-bmdwmc') !== $thistr.attr('data-mindwmc')) {
                $("#dw").append("<option data-zhyz='1' value='" + $thistr.attr('data-zxdwId') + "'>" + $thistr.attr('data-mindwmc') + "</option>");   //出库单位
            }
            $("#dw").append("<option data-zhyz='" + $thistr.attr('data-zhyz') + "' value='" + $thistr.attr('data-bmdwId') + "'>" + $thistr.attr('data-bmdwmc') + "</option>");   //出库单位
            $("#mindwmc").val($thistr.attr('data-zxdwId')).attr("data-zhyz", 1).attr("data-name", $thistr.attr('data-mindwmc'));
            $("#bmdwmc").val($thistr.attr('data-bmdwId')).attr("data-zhyz", $thistr.attr('data-zhyz')).attr("data-name", $thistr.attr('data-bmdwmc'));
            $("span[name=sp_bmdw]").html("(元/" + $thistr.attr('data-bmdwmc') + ")").attr("data-zhyz", $thistr.attr('data-zhyz'));
            $("#slstr").val($thistr.attr('data-slstr'));
            $("#kykcsl").val($thistr.attr('data-kykcsl'));
            $("#gg").val($thistr.attr('data-gg'));
            $("#lsj").val(parseFloat($thistr.attr('data-bmlsj')).toFixed(4));
            $("#lbmc").val($thistr.attr('data-lbmc'));
            $("#sccj").val($thistr.attr('data-supplierName'));
            $("#minlsj").val($thistr.attr('data-minlsj'));
            $("#sl").val("");
            $("#zje").val("");
            $("#lsze").val("");
            $("#dw").trigger("change");
            return;
        }
    });

    //物资批次选择
    $("#pc").newtouchBatchFloatingSelector({
        clickautotrigger: true,
        height: 300,
        width: 870,
        id: 'selectProductPcPh',
        url: '/StorageManage/Storage/ProductBatchQuery',
        ajaxmethod: 'Get',
        ajaxreqdata: function () {
            var reqData = {};
            reqData.proId = $("#productId").val();
            reqData.deliveryNo = $("#deliveryNo").val();
            reqData.gysId = $("#rkbmId").val();
            return reqData;
        },
        caption: "物资批号批次信息",
        colModel: [
            { label: '批号', name: 'ph', width: 120 },
            { label: '批次', name: 'pc', width: 200 },
            { label: '数量', name: 'slstr', width: 100 },
            {
                label: '进价', name: 'bmjj', width: 90,
                formatter: function (cellvalue) { return cellvalue.toFixed(4); }
            },
            {
                label: '进价总额', name: 'jjzje', width: 90,
                formatter: function (cellvalue) { return cellvalue.toFixed(2); }
            },
            { label: '有效期', name: 'yxq', width: 100, formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
            { label: '生产日期', name: 'scrq', width: 100, formatter: "date", formatoptions: { srcformat: 'Y-m-d', newformat: 'Y-m-d' } },
            { label: 'kykcsl', name: 'kykcsl', hidden: true },
            { label: 'fph', name: 'fph', hidden: true },
            { label: 'kcsl', name: 'kcsl', hidden: true }
        ],
        itemdbclickhandler: function ($thistr) {
            $("#ph").val($thistr.attr('data-ph'));
            $("#pc").val($thistr.attr('data-pc'));
            $("#slstr").val($thistr.attr('data-slstr'));
            $("#jj").val($thistr.attr('data-jj'));
            $("#yxq").val($thistr.attr('data-yxq'));
            $("#scrq").val($thistr.attr('data-scrq'));
            $("#kykcsl").attr('data-kcsl', $thistr.attr('data-kcsl')).val($thistr.attr('data-kykcsl'));
            $("#fph").val($thistr.attr('data-fph'));
            return;
        }
    });

    //回车加载grid
    $('#remark').keydownEnterEvent(function () {
        if ($('#editRowId').val()) {            //是修改
            newtouch_event_f3();    //模拟 修改按钮 点击
        }
        else {
            newtouch_event_f2();    //模拟 确认按钮 点击
        }
    });

    //切换配送单
    function ChangeDeliveryNo() {
        $("#wzmc").val("");
        $("#productId").val("");
        $("#lbmc").val("");
        $("#sl").val("");
        $("#slstr").val("");
        $("#ph").val("");
        $("#pc").val("");
        $("#jj").val("");
        $("#lsj").val("");
        $("#zje").val("");
        $("#lsze").val("");
        $("#kykcsl").val("").attr("data-kcsl", "");
        $("#dw").empty();
        $("#txtCKZE").val("");
        $("#mindwmc").val("").attr("data-zhyz", "").attr("data-name", "");
        $("#bmdwmc").val("").attr("data-zhyz", "").attr("data-name", "");
        $("#yxq").val("");
        $("#zhyz").val("");
        $("#gg").val("");
        $("#wzmc").val("");
        $("#remark").val("");
        $("#sccj").val("");
        $("#scrq").val("");
        $("span[name=sp_bmdw]").html("");
        $("#minlsj").val("");
        $("#fph").val("");
        cleanEditRowId();
    }

    //清空input标签
    function CleanInput() {
        $("#deliveryNo").val("");
        $("#rkbmId").val("");
        $("#rkbmName").val("");
        ChangeDeliveryNo();
    }

    //数量失焦,计算批发金额 零售金额
    function CKSL_Leave() {
        if ($("#wzmc").val() === "") {
            return false;
        }
        var minlsj = parseFloat($("#minlsj").val());
        var zhyz = parseFloat($("#dw").find("option:selected").attr("data-zhyz"));
        var lsj = parseFloat(minlsj * zhyz).toFixed(4);
        $("#lsj").val(lsj);
        $("#zhyz").val(zhyz);
        if ($("#sl").val() === "") {
            return false;
        }
        if (isNaN($("#sl").val())) {
            $.modalMsg("数量格式错误", 'warning', 1500);
            return false;
        }
        var sl = parseFloat($("#sl").val());
        $("#lsze").val((minlsj * zhyz * sl).toFixed(2));
        CKJE_Leave();//重算入库单价
        return true;
    }

    //出库金额失焦,计算出库单价
    function CKJE_Leave() {
        if (isNaN($("#zje").val())) {
            $.modalMsg("进价总额格式错误", 'warning', 1500);
            return false;
        }
        if ($("#zje").val() === "") {
            return false;
        }
        if ($("#sl").val() === "") {
            return false;
        }
        var sl = parseFloat($("#sl").val());;
        var jjze = parseFloat($("#zje").val());
        var jj = sl === 0 ? 0 : jjze / sl;
        $("#jj").val(parseFloat(jj).toFixed(4));
        return true;
    }

    //计算出库总额
    function ComputeCKZE() {
        var ckze = 0;
        var count = $("#gridCKMX").getGridParam('records');
        if (count > 0) {
            var rowIds = $("#gridCKMX").getDataIDs();
            for (var i = 0; i < rowIds.length; i++) {
                var getRowZje = $("#gridCKMX").getRowData(rowIds[i]).zje;
                ckze = parseFloat(ckze) + parseFloat(getRowZje);
            }
            $("#txtCKZE").html(ckze.toFixed(2));//出库金额
        } else {
            $("#txtCKZE").html('');
        }
    }

    //数据校验
    function DataValate() {
        if ($("#wzmc").val() === "") {
            $.modalMsg("请选择物资", 'warning', 1500);
            return false;
        }
        if ($("#ph").val() === "") {
            $.modalMsg("请选择批号", 'warning', 1500);
            return false;
        }
        if ($("#sl").val() === "") {
            $.modalMsg("入库数量不能为空", 'warning', 1500);
            return false;
        }
        if (!IsNum($("#zje").val()) || $("#zje").val() === "") {
            $.modalMsg("出库金额为大于0的数字", 'warning', 1500);
            return false;
        }
        var zhyz = parseFloat($("#dw").find("option:selected").attr("data-zhyz"));
        var sl = parseFloat($("#sl").val());
        var kykcsl = parseFloat($("#kykcsl").val());
        if (sl * zhyz > kykcsl) {
            $.modalMsg("出库数量不能大于可用库存", 'warning', 1500);
            return false;
        }
        return true;
    }

    function IsNum(value) {
        var patrn = /^(-)?\d+(\.\d+)?$/;
        if (patrn.exec(value) == null || value === "") {
            return false;
        } else {
            return true;
        }
    }

    //禁用
    function DisableControls() {
        $("#deliveryNo").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
        $("#crkfs").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
        $("#rkbmName").attr("disabled", "disabled").css("background-color", "rgb(238, 238, 238)");
    }

    //启用
    function EnabledSysBasicInfo() {
        $("#deliveryNo").removeAttr("disabled").removeAttr("style");
        $("#crkfs").removeAttr("disabled").removeAttr("style");
        $("#rkbmName").removeAttr("disabled").removeAttr("style");
    }

    //删除保存的当前选中行ID
    function cleanEditRowId() {
        $("#editRowId").val("");
        //启用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f2' });
        readonly(list, 2);
        //禁用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f3' });
        list.push({ id: 'btn_bottombutton_f6' });
        readonly(list, 1);
    }

    //记录当前选中行ID
    function RemarkEditRowId(rowId) {
        $("#editRowId").val(rowId);
        //启用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f3' });
        list.push({ id: 'btn_bottombutton_f6' });
        readonly(list, 2);
        //禁用按钮
        var list = new Array();
        list.push({ id: 'btn_bottombutton_f2' });
        readonly(list, 1);
    }
</script>
